<?php

namespace App\Jobs;

use App\Exceptions\Arr;
use App\Models\Appoint;
use App\Models\AppointNum;
use App\Models\HospitalUserFamily;
use App\Models\Notice;
use App\Models\TempPushRecord;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class HospitalNotice implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $notice;
    public $tries = 0;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Notice $notice)
    {
        $this->notice = $notice;
        $this->onQueue('hospital_notice');
        $this->onConnection('redis');
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $notice = $this->notice;
        if($notice->type == 1){
            $user_ids = HospitalUserFamily::query()
                ->where('hospital_id',$notice->hospital_id)
                ->where('status',1)
                ->groupBy('user_id')
                ->pluck('user_id')->toArray();
        }elseif ($notice->type == 2){
            $user_ids = HospitalUserFamily::query()
                ->where('hospital_id',$notice->hospital_id)
                ->where('status',1)
                ->where('type',1)
                ->groupBy('user_id')
                ->pluck('user_id')->toArray();
        }elseif ($notice->type == 3){
            $user_ids = HospitalUserFamily::query()
                ->where('hospital_id',$notice->hospital_id)
                ->where('status',1)
                ->where('type',2)
                ->groupBy('user_id')
                ->pluck('user_id')->toArray();
        }
//        $data = [];
//        foreach($user_ids as $user_id){
//            $row['user_id'] = $user_id;
//            $row['temp_id'] = 1;
//            $data[]=$row;
//        }
        if($user_ids){
            try {
                //TempPushRecord::upsert($data,['user_id','temp_id']);

                foreach($user_ids as $k=>$v){

                    $tempPushRecord = TempPushRecord::createTemp($v,1);
                    TempPush::dispatch($tempPushRecord,$notice);
                }
                $notice->status = 2;
                $notice->save();
            }catch (\Exception $e){
                $notice->status = 3;
                $notice->save();
                Log::error($e->getMessage());
                $this->fail($e->getMessage());
            }
        }else{
            $notice->status = 2;
            $notice->save();
            $this->fail('此类型用户为空！');
        }
    }
//    /**
//     * 获取作业的唯一 ID。
//     */
//    public function uniqueId(): string
//    {
//        return $this->appoint->hospital_id."-".$this->appoint->type;
//    }

    /**
     * 同一条数据同一时间只处理一次。
     *
     * @return array<int, object>
     */
    public function middleware(): array
    {
        return [(new WithoutOverlapping($this->notice->id))->dontRelease()];
    }
}
